package cn.agiledata.bank.information.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.hibernateBean.MobileChargeRecord;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class MobileChargeDaoImpl extends IPBHibernate3BaseMgr implements MobileChargeDao {

	public MobileChargeRecord saveOrUpdateRecored(MobileChargeRecord record) {
		// 创建或者修改充值记录
		if(record == null) {
			return null;
		}
		
		getHibernateTemplate().saveOrUpdate(record);
		
		return record;
	}

	public MobileChargeRecord getRecordByNo(String recordNo) {
		MobileChargeRecord record = null;
		// 根据充值编号取得充值记录
		String strOql = "select mcr from MobileChargeRecord mcr " +
				"where mcr.recordNo='" + recordNo + "'";
		
		List listResult = getHibernateTemplate().find(strOql);
		
		if(listResult != null && listResult.size() > 0) {
			record = (MobileChargeRecord)listResult.get(0);
		}
		
		return record;
	}
	
	public int getCurrentPayTime(Long  accountId) {
		String hql = "select count(*) from MobileChargeRecord mcr where mcr.transaction.credit.account.id=11 and mcr.accountSeq=" + accountId;
		hql += " and to_char(mcr.createTime,'yyyy-MM-dd') = '" + DateUtil.date2String(new Date()) + "' and (mcr.status = 2 or mcr.status = 3)";
		Query q = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		return ((Long) q.uniqueResult()).intValue();
	}
}
